package com.seaboxdata.interfaceJob.yxzsxt;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.seaboxdata.entity.yxzsxt.IntersectionDelayDetails;
import com.seaboxdata.entity.yxzsxt.LinkDataForTgis;
import com.seaboxdata.kafka.BaseStringKafkaProducer;
import com.seaboxdata.utils.TransRealHttpsHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName IntersectionDelayDetailsJob
 * @Deacription TODO 获取某一交叉口各个方向的延误信息(转向信息)
 * @Author dhjie
 * @Date 2021/8/26
 * @Version 1.0
 **/
@Component
@Configuration
@Slf4j
public class IntersectionDelayDetailsJob {

    @Value("${real.trans.url.IntersectionDelayDetails}")
    private String url;

    @Value("${real.trans.token.IntersectionDelayDetails}")
    private String token;

    @Value("${real.trans.IntersectionDelayDetailsTopic}")
    private String topic;

    @Value("${interface.isTest}")
    private String isTest;

    @Autowired
    private BaseStringKafkaProducer baseStringKafkaProducer;

    private static final int retry = 3;

    private static int countRetry = 0;

    @Scheduled(cron = "${real.trans.IntersectionDelayDetailsScheduled}")//每隔5分钟执行一次
    public void doCollect() {
        //如果是测试,直接退出
        if("true".equals(isTest)){
            return;
        }
        log.info("调用 获取某一交叉口各个方向的延误信息 start");
        try {
            List<Integer> param = new ArrayList<Integer>() {
                {
                    add(6811);
                    add(6919);
                    add(4708);
                    add(4712);
                    add(4890);
                    add(6822);
                    add(4545);
                    add(4700);
                    add(4904);
                }
            };
            String fids = String.join(",", param.stream().map(String::valueOf).collect(Collectors.toList()));
//            for (Integer fid:param) {
                List<IntersectionDelayDetails> roadOperationMonitorings = retryGetData(url,fids, token);
                if (!CollectionUtils.isEmpty(roadOperationMonitorings)) {
                    //写入kafka
                    Date date = new Date();
                    for (IntersectionDelayDetails roadOperationMonitoring : roadOperationMonitorings) {
                        roadOperationMonitoring.setCjsj(date);
                        baseStringKafkaProducer.sendMessageToKafka(JSONObject.toJSONString(roadOperationMonitoring), topic);
                    }
                }
//            }
        } catch (Exception e) {
            log.error("调用 获取某一交叉口各个方向的延误信息 异常");
            e.printStackTrace();
        }
    }

    public List<IntersectionDelayDetails> retryGetData(String url,String fid, String token) {

        List<IntersectionDelayDetails> roadOperationMonitorings = null;
        JSONObject jsonParam=new JSONObject();
        jsonParam.put("intersection_fid",fid);
        try {
            roadOperationMonitorings = TransRealHttpsHandler.sendPostParam(url, token, jsonParam, IntersectionDelayDetails.class);
            log.info("roadOperationMonitorings======{}",roadOperationMonitorings.size());
            if (!(CollectionUtils.isNotEmpty(roadOperationMonitorings) && roadOperationMonitorings.get(0) != null)) {
                if (countRetry == retry) {
                    log.error("we have try {} to getData,but still have error", countRetry);
                    return null;
                } else {
                    countRetry++;
                    Thread.sleep(1000);
                    roadOperationMonitorings = retryGetData(url,fid, token);
                }
            } else {
                countRetry = 0;
            }
        } catch (Exception e) {
            log.error("调用 获取某一交叉口各个方向的延误信息 异常");
            e.printStackTrace();
        }
        return roadOperationMonitorings;
    }

}
